<script setup>
import { ref, reactive, watch } from 'vue'
import { SelectFolder, ClearAllCache } from '../../wailsjs/go/main/App'

const props = defineProps({
  config: {
    type: Object,
    required: true
  }
})

const emit = defineEmits(['close', 'update', 'refresh-tree'])

const localConfig = reactive({ ...props.config })

watch(() => props.config, (newConfig) => {
  Object.assign(localConfig, newConfig)
}, { deep: true })

const selectTargetFolder = async () => {
  try {
    const selectedPath = await SelectFolder()
    if (selectedPath) {
      localConfig.targetFolder = selectedPath
    }
  } catch (error) {
    console.error('Failed to select folder:', error)
  }
}

const saveSettings = () => {
  emit('update', { ...localConfig })
  emit('close')
}

const resetToDefaults = () => {
  localConfig.targetFolder = getDesktopPath()
  localConfig.darkMode = false
  localConfig.globalHotkey = 'Ctrl+Shift+F'
  localConfig.autoHide = true
  localConfig.hideOnEdge = true
}

const refreshFileTree = () => {
  emit('refresh-tree')
}

const clearAllCache = async () => {
  try {
    await ClearAllCache()
    // Trigger a refresh after clearing cache
    emit('refresh-tree')
  } catch (error) {
    console.error('Failed to clear cache:', error)
  }
}

const getDesktopPath = () => {
  // This would typically come from the backend
  return 'C:\\Users\\' + (window.navigator.userAgent.includes('Windows') ? 'Desktop' : 'Desktop')
}

const closeSettings = () => {
  emit('close')
}
</script>

<template>
  <div class="settings-overlay" @click="closeSettings">
    <div class="settings-modal" @click.stop>
      <div class="settings-header">
        <h2 class="settings-title">
          <svg class="icon" viewBox="0 0 24 24" fill="currentColor">
            <path d="M12,15.5A3.5,3.5 0 0,1 8.5,12A3.5,3.5 0 0,1 12,8.5A3.5,3.5 0 0,1 15.5,12A3.5,3.5 0 0,1 12,15.5M19.43,12.97C19.47,12.65 19.5,12.33 19.5,12C19.5,11.67 19.47,11.34 19.43,11L21.54,9.37C21.73,9.22 21.78,8.95 21.66,8.73L19.66,5.27C19.54,5.05 19.27,4.96 19.05,5.05L16.56,6.05C16.04,5.66 15.5,5.32 14.87,5.07L14.5,2.42C14.46,2.18 14.25,2 14,2H10C9.75,2 9.54,2.18 9.5,2.42L9.13,5.07C8.5,5.32 7.96,5.66 7.44,6.05L4.95,5.05C4.73,4.96 4.46,5.05 4.34,5.27L2.34,8.73C2.22,8.95 2.27,9.22 2.46,9.37L4.57,11C4.53,11.34 4.5,11.67 4.5,12C4.5,12.33 4.53,12.65 4.57,12.97L2.46,14.63C2.27,14.78 2.22,15.05 2.34,15.27L4.34,18.73C4.46,18.95 4.73,19.03 4.95,18.95L7.44,17.94C7.96,18.34 8.5,18.68 9.13,18.93L9.5,21.58C9.54,21.82 9.75,22 10,22H14C14.25,22 14.46,21.82 14.5,21.58L14.87,18.93C15.5,18.68 16.04,18.34 16.56,17.94L19.05,18.95C19.27,19.03 19.54,18.95 19.66,18.73L21.66,15.27C21.78,15.05 21.73,14.78 21.54,14.63L19.43,12.97Z" />
          </svg>
          设置
        </h2>
        <button @click="closeSettings" class="close-btn">
          <svg class="icon" viewBox="0 0 24 24" fill="currentColor">
            <path d="M19,6.41L17.59,5L12,10.59L6.41,5L5,6.41L10.59,12L5,17.59L6.41,19L12,13.41L17.59,19L19,17.59L13.41,12L19,6.41Z" />
          </svg>
        </button>
      </div>

      <div class="settings-content">
        <div class="settings-section">
          <h3 class="section-title">基本设置</h3>
          
          <div class="setting-item">
            <label class="setting-label">目标文件夹</label>
            <div class="setting-control">
              <div class="folder-input-group">
                <input 
                  v-model="localConfig.targetFolder"
                  class="folder-input"
                  type="text"
                  placeholder="选择目标文件夹"
                  readonly
                />
                <button @click="selectTargetFolder" class="folder-select-btn">
                  <svg class="icon" viewBox="0 0 24 24" fill="currentColor">
                    <path d="M10,4H4C2.89,4 2,4.89 2,6V18A2,2 0 0,0 4,20H20A2,2 0 0,0 22,18V8C22,6.89 21.1,6 20,6H12L10,4Z" />
                  </svg>
                  选择
                </button>
              </div>
              <p class="setting-description">设置默认搜索的文件夹路径</p>
            </div>
          </div>

          <div class="setting-item">
            <label class="setting-label">全局快捷键</label>
            <div class="setting-control">
              <input 
                v-model="localConfig.globalHotkey"
                class="hotkey-input"
                type="text"
                placeholder="Ctrl+Shift+F"
              />
              <p class="setting-description">用于显示/隐藏应用窗口的快捷键</p>
            </div>
          </div>
        </div>

        <div class="settings-section">
          <h3 class="section-title">外观设置</h3>
          
          <div class="setting-item">
            <div class="setting-control">
              <label class="checkbox-label">
                <input 
                  v-model="localConfig.darkMode"
                  type="checkbox"
                  class="checkbox"
                />
                <span class="checkbox-text">深色模式</span>
              </label>
              <p class="setting-description">启用深色主题界面</p>
            </div>
          </div>
        </div>

        <div class="settings-section">
          <h3 class="section-title">窗口行为</h3>
          
          <div class="setting-item">
            <div class="setting-control">
              <label class="checkbox-label">
                <input 
                  v-model="localConfig.autoHide"
                  type="checkbox"
                  class="checkbox"
                />
                <span class="checkbox-text">失去焦点时自动隐藏</span>
              </label>
              <p class="setting-description">当应用失去焦点时自动隐藏窗口</p>
            </div>
          </div>

          <div class="setting-item">
            <div class="setting-control">
              <label class="checkbox-label">
                <input 
                  v-model="localConfig.hideOnEdge"
                  type="checkbox"
                  class="checkbox"
                />
                <span class="checkbox-text">靠近屏幕边缘时收起</span>
              </label>
              <p class="setting-description">当窗口靠近屏幕边缘时自动收起隐藏</p>
            </div>
          </div>
        </div>

        <div class="settings-section">
          <h3 class="section-title">缓存管理</h3>
          
          <div class="setting-item">
            <label class="setting-label">文件树缓存</label>
            <div class="setting-control">
              <div class="cache-actions">
                <button @click="refreshFileTree" class="cache-btn refresh-btn">
                  <svg class="icon" viewBox="0 0 24 24" fill="currentColor">
                    <path d="M17.65,6.35C16.2,4.9 14.21,4 12,4A8,8 0 0,0 4,12A8,8 0 0,0 12,20C15.73,20 18.84,17.45 19.73,14H17.65C16.83,16.33 14.61,18 12,18A6,6 0 0,1 6,12A6,6 0 0,1 12,6C13.66,6 15.14,6.69 16.22,7.78L13,11H20V4L17.65,6.35Z" />
                  </svg>
                  重新扫描
                </button>
                <button @click="clearAllCache" class="cache-btn clear-btn">
                  <svg class="icon" viewBox="0 0 24 24" fill="currentColor">
                    <path d="M19,4H15.5L14.5,3H9.5L8.5,4H5V6H19M6,19A2,2 0 0,0 8,21H16A2,2 0 0,0 18,19V7H6V19Z" />
                  </svg>
                  清除缓存
                </button>
              </div>
              <p class="setting-description">重新扫描当前文件夹或清除所有缓存数据</p>
            </div>
          </div>
        </div>
      </div>

      <div class="settings-footer">
        <button @click="resetToDefaults" class="reset-btn">
          恢复默认
        </button>
        <div class="footer-actions">
          <button @click="closeSettings" class="cancel-btn">
            取消
          </button>
          <button @click="saveSettings" class="save-btn">
            保存
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.settings-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  backdrop-filter: blur(4px);
}

.settings-modal {
  background: var(--modal-bg);
  border-radius: 12px;
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
  width: 90%;
  max-width: 500px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.settings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px;
  border-bottom: 1px solid var(--border-color);
  background: var(--header-bg);
}

.settings-title {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: var(--text-color);
}

.close-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: var(--text-color);
  cursor: pointer;
  transition: all 0.2s ease;
}

.close-btn:hover {
  background: var(--hover-bg);
}

.settings-content {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
}

.settings-section {
  margin-bottom: 32px;
}

.settings-section:last-child {
  margin-bottom: 0;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  margin: 0 0 16px 0;
  color: var(--text-color);
}

.setting-item {
  margin-bottom: 20px;
}

.setting-item:last-child {
  margin-bottom: 0;
}

.setting-label {
  display: block;
  font-size: 14px;
  font-weight: 500;
  margin-bottom: 8px;
  color: var(--text-color);
  text-align: left;
}

.setting-control {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.folder-input-group {
  display: flex;
  gap: 8px;
}

.folder-input {
  flex: 1;
  height: 40px;
  padding: 0 12px;
  border: 1px solid var(--input-border);
  border-radius: 6px;
  background: var(--input-bg);
  color: var(--input-color);
  font-size: 14px;
  transition: all 0.2s ease;
}

.folder-input:focus {
  outline: none;
  border-color: var(--focus-border);
  box-shadow: 0 0 0 2px var(--focus-shadow);
}

.folder-select-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 40px;
  padding: 0 16px;
  border: 1px solid var(--input-border);
  border-radius: 6px;
  background: var(--button-bg);
  color: var(--button-color);
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s ease;
  white-space: nowrap;
}

.folder-select-btn:hover {
  background: var(--button-hover-bg);
}

.hotkey-input {
  width: 200px;
  height: 40px;
  padding: 0 12px;
  border: 1px solid var(--input-border);
  border-radius: 6px;
  background: var(--input-bg);
  color: var(--input-color);
  font-size: 14px;
  transition: all 0.2s ease;
}

.hotkey-input:focus {
  outline: none;
  border-color: var(--focus-border);
  box-shadow: 0 0 0 2px var(--focus-shadow);
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  font-size: 14px;
  color: var(--text-color);
}

.checkbox {
  width: 16px;
  height: 16px;
  border: 2px solid var(--input-border);
  border-radius: 3px;
  background: var(--input-bg);
  cursor: pointer;
  transition: all 0.2s ease;
}

.checkbox:checked {
  background: var(--primary-color);
  border-color: var(--primary-color);
}

.checkbox-text {
  font-weight: 500;
}

.setting-description {
  font-size: 12px;
  color: var(--secondary-color);
  margin: 0;
  line-height: 1.4;
  text-align: left;
}

.settings-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px;
  border-top: 1px solid var(--border-color);
  background: var(--header-bg);
}

.footer-actions {
  display: flex;
  gap: 12px;
}

.reset-btn,
.cancel-btn,
.save-btn {
  height: 36px;
  padding: 0 16px;
  border: 1px solid var(--input-border);
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
}

.reset-btn {
  background: transparent;
  color: var(--secondary-color);
}

.reset-btn:hover {
  background: var(--hover-bg);
}

.cancel-btn {
  background: var(--button-bg);
  color: var(--button-color);
}

.cancel-btn:hover {
  background: var(--button-hover-bg);
}

.save-btn {
  background: var(--primary-color);
  color: white;
  border-color: var(--primary-color);
}

.save-btn:hover {
  background: var(--primary-hover);
  border-color: var(--primary-hover);
}

.cache-actions {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  justify-content: center;
}

.cache-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 36px;
  padding: 0 16px;
  border: 1px solid var(--input-border);
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
}

.refresh-btn {
  background: var(--primary-color);
  color: white;
  border-color: var(--primary-color);
}

.refresh-btn:hover {
  background: var(--primary-hover);
  border-color: var(--primary-hover);
}

.clear-btn {
  background: #ef4444;
  color: white;
  border-color: #ef4444;
}

.clear-btn:hover {
  background: #dc2626;
  border-color: #dc2626;
}

.icon {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}

/* Light theme */
.light-theme .settings-modal {
  --modal-bg: #ffffff;
  --header-bg: #f9fafb;
  --text-color: #111827;
  --secondary-color: #6b7280;
  --border-color: #e5e7eb;
  --hover-bg: #f3f4f6;
  --input-border: #d1d5db;
  --input-bg: #ffffff;
  --input-color: #374151;
  --focus-border: #3b82f6;
  --focus-shadow: rgba(59, 130, 246, 0.1);
  --button-bg: #f9fafb;
  --button-color: #374151;
  --button-hover-bg: #f3f4f6;
  --primary-color: #3b82f6;
  --primary-hover: #2563eb;
}

/* Dark theme */
.dark-theme .settings-modal {
  --modal-bg: #1f2937;
  --header-bg: #111827;
  --text-color: #f9fafb;
  --secondary-color: #9ca3af;
  --border-color: #374151;
  --hover-bg: #374151;
  --input-border: #4b5563;
  --input-bg: #374151;
  --input-color: #f9fafb;
  --focus-border: #60a5fa;
  --focus-shadow: rgba(96, 165, 250, 0.1);
  --button-bg: #374151;
  --button-color: #d1d5db;
  --button-hover-bg: #4b5563;
  --primary-color: #3b82f6;
  --primary-hover: #2563eb;
}
</style>